package com.dd.admin.config;

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dd.admin.system.setting.service.SystemSettingService;

/**
 * MinIO配置类
 *
 * @author 727869402@qq.com
 * @since 2024-05-23
 */
@Configuration
public class MinioConfig {

    @Value("${minio.endpoint:}")
    private String defaultEndpoint;

    @Value("${minio.accessKey:}")
    private String defaultAccessKey;

    @Value("${minio.secretKey:}")
    private String defaultSecretKey;

    @Value("${minio.bucketName:}")
    private String defaultBucketName;

    @Autowired(required = false)
    private SystemSettingService systemSettingService;

    @Bean
    public MinioClient minioClient() {
        String endpoint = getMinioConfig("MINIO_ENDPOINT", defaultEndpoint);
        String accessKey = getMinioConfig("MINIO_ACCESS_KEY", defaultAccessKey);
        String secretKey = getMinioConfig("MINIO_SECRET_KEY", defaultSecretKey);
        
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
    
    private String getMinioConfig(String key, String defaultValue) {
        if (systemSettingService != null) {
            String value = systemSettingService.getSettingValue(key);
            return value != null ? value : defaultValue;
        }
        return defaultValue;
    }
}